<template>
	<view class="content">
		<view class="price-title ft42">欢迎登录</view>
		<u-field
			v-model="workNumber"
			label="工号"
			placeholder="请填写工号"
		>
		</u-field>
		<u-field
			v-model="phone"
			label="手机号"
			placeholder="请填写手机号"
		>
		</u-field>
		<u-field
			v-model="code"
			label="验证码"
			placeholder="请填写验证码"
		>
			<u-button size="default" slot="right" type="success" @click="getVerification">{{tips}}</u-button>
		</u-field>
		<u-verification-code ref="uCode" @change="codeChange"></u-verification-code>
		<view class="mg100-x gap">
		      <u-button @click="register" type="success" shape="circle" >{{registerButton}}</u-button>
		</view>
		<u-toast ref="uToast"/>
	</view>
</template>

<script>
import { getUploadParamList,getVerificationCode,postRegisterInfo,accessPersonInfo,findFeedbackJobNumber } from '../../utils/index'
	export default {
	data() {
		return {
			tips: '获取验证码',
			registerButton:'注册',
			code: '',							//验证码
			workNumber:'',						//员工工号
			phone:''							//手机号码
		}
	},
	methods: {
		// 该函数对button中的内容进行修改
		codeChange(text){
			this.tips = text;
		},
		// 发送验证码按钮绑定的函数
		async getVerification(){
			if(this.$refs.uCode.canGetCode){
				const {data} = await getVerificationCode({phone:this.phone});
				uni.showLoading({
					title:'正在获取验证码'
				})
				setTimeout(() => {
					uni.hideLoading();
					this.$u.toast('验证码已发送');
					this.$refs.uCode.start();
				},1000);
			}else{
				this.$u.toast('倒计时结束后再发送')
			}
		},
		// 注册按钮，发送注册信息
		async register(){
				const data = await postRegisterInfo({
					phone:this.phone,
					jobNumber:this.workNumber,
					code:this.code,
					openId:uni.getStorageSync('openId')
				})
				const errMsg = data.data.msg
				if(data.data.code=='1')
				{
					this.$refs.uToast.show({
						title:'注册成功',
						type:'success',
						duration:3000,
					})
					uni.setStorageSync('Authorization', data.data.data)
					this.getUserInfo()
				}else{
					this.$refs.uToast.show({
						title:errMsg,
						type:'warning',
						duration:2000
					})
				}
		},
		// 登录函数
		async paramListLogin(){
			let [a,{code, errMsg }] = await uni.login({ provider:'weixin' })
			if(errMsg === "login:ok"){
				// 调取后端接口，将code传入，换取openId与token
				const {data} = await getUploadParamList({code:code})
				if(data.code=="1"){
					// 未注册过显示注册	
					uni.setStorageSync('openId', data.data.openId)
					if(data.data.Authorization==null){
						this.$refs.uToast.show({
							title:'请先注册账号',
							type:'warning',
							duration:2000,
						})
					}else{
						uni.setStorageSync('Authorization', data.data.Authorization)
						this.getUserInfo()
					}
				}
			}
		},
		// 检查用户是否有token
		// 存在token则获取对应的用户信息，不存在就进行注册
		checkAuthorization(){
				const value = uni.getStorageSync('Authorization')
				if(value==''){
					this.paramListLogin()
				}else{
					this.getUserInfo()
				}
			},
		// 判断装配车间员工还是数控车间员工
		// 普通员工的roleID是相同的，不同的是departmentName
		judgeDepartment(data) {
			if (data == '数控车间' || data == '焊接车间' || data == '喷涂车间') {
				uni.redirectTo({
					url: '../CNCstaff/home'
				})
			} else {
				uni.redirectTo({
					url: '../staff/home/home'
				})
			}
		},
		// 获取用户roleId信息，来决定用户跳转到不同的页面
		// 该跳转是不可以返回的，使用的是uni.redirectTo这个API
		// 优化：使用switch语句来代替if-else语句
		async getUserInfo() {
			const {data} = await accessPersonInfo()
			switch(data.code) {
				case 2: {
					this.paramListLogin()
				}
				break;
				case 9: {
					this.$refs.uToast.show({
						title:'账号已禁用，请联系管理员',
						type:'warning',
						duration:2000
					})
				}
				break;
				case 1:{
					uni.setStorageSync('roleId',data.data.roleId)
					uni.setStorageSync('departmentName',data.data.departmentName)
					uni.setStorageSync('user',data.data)
					// 判断角色类型用以跳转不同的页面
					switch(data.data.roleId) {
						case 8: {
							this.judgeDepartment(data.data.departmentName)
						}
						break;
						case 1: {
							uni.redirectTo({
								url: '../craft/home'
							})
						}
						break;
						case 7: {
							uni.redirectTo({
								url: '../QE/home'
							})
						}
						break;
						case 9: {
							uni.redirectTo({
								url: '../leader/home/home'
							})
						}
						break;
						case 15: {
							uni.redirectTo({
								url: '../director/home'
							})
						}
						break;
						case 16: {
							uni.redirectTo({
								url: '../PMC/home'
							})
						}
						break;
						case 5: {
							uni.redirectTo({
								url: '../EC/home/home'
							})
						}
						break;
						case 6: {
							uni.redirectTo({
								url: '../FC/home/home'
							})
						}
						break;
						case 3: {
							uni.redirectTo({
								url: '../QC/home/home'
							})
						}
						break;
						case 23: {
							uni.redirectTo({
								url: '../quality/home/home'
							})
						}
						break;
						case 24: {
							uni.redirectTo({
								url: '../quality/home/home'
							})
						}
						break;
						case 30: {
							uni.redirectTo({
								url: '../quality/home/home'
							})
						}
						break;
						case 26: {
							uni.redirectTo({
								url: '../staffs/home/home'
							})
						}
						break;
						case 10: {
							uni.redirectTo({
								url: '../QC/home/home'
							})
						}
						break;
						case 11: {
							uni.redirectTo({
								url: '../QC/home/home'
							})
						}
						break;
						default: {
							uni.redirectTo({
								url: '../../publicPage/blank'
							})
						}
						break;
				}
			}
			}
			if(data.data.departmentName == 'EMS总装车间'){
				this.findFeedbackjobNumber(data.data.jobNumber)
			}
		},
		async findFeedbackjobNumber(val){
			uni.setStorageSync('feedbackForm','')
			const {data} = await findFeedbackJobNumber()
			if(data.code == '1') {
				data.data.forEach((item,index) => {
					if(item == val) {
						console.log(111);
						uni.setStorageSync('feedbackForm',item)
					}
				})
			}
		}
		},
		// 在页面显示的时候调用生命周期函数检查token
		onShow(){
			this.checkAuthorization();
		},
	}
</script>

<style lang="scss" scoped>
// “注册”按钮距离上面表单的距离
.gap {
	margin-top: 80rpx;
}
.content {
  text-align: center;
  width: 100%;
  height: 100vh;
}
// “欢迎登录”样式
.price-title {
  font-size: 40rpx;
  font-weight: bold;
  line-height: 120rpx;
}
</style>